package com.xiaoma.tpo.service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.ClsSentenceDao;
import com.xiaoma.tpo.dao.StGroupDao;
import com.xiaoma.tpo.dao.StPlanDao;
import com.xiaoma.tpo.dao.StScoreDao;
import com.xiaoma.tpo.dao.StSentenceDao;
import com.xiaoma.tpo.domain.ClsSentence;
import com.xiaoma.tpo.domain.StGroup;
import com.xiaoma.tpo.domain.StPlan;
import com.xiaoma.tpo.domain.StScore;
import com.xiaoma.tpo.domain.StSentence;
import com.xiaoma.tpo.utils.Pager;

@Service
public class SentenceService {

    @Autowired
    private StPlanDao planDao;
    
    @Autowired
    private StGroupDao groupDao;
    
    @Autowired
    private StSentenceDao sentenceDao;
   
    @Autowired
    private StScoreDao scoreDao;
    @Autowired
    private ClsSentenceDao clsSentenceDao;
    
    public List<StPlan> findAllPlans() {
        return planDao.findAll();
    }
    
    public List<StGroup> findGroupsByPlanId(int stPlanId) {
        StGroup entity = new StGroup();
        entity.setStPlanId(stPlanId);
        return groupDao.findByColumns(entity);
    }
    
    public List<StSentence> findSentencesByGroupId(int stGroupId) {
        StSentence entity = new StSentence();
        entity.setStGroupId(stGroupId);
        return sentenceDao.findByColumns(entity);
    }
    
    public int findGroupCountByPlanId(int planId) {
        StGroup entity = new StGroup();
        entity.setStPlanId(planId);
        return groupDao.queryCount(entity);
    }
    
    public int findSentenceCountByGroupId(int groupId) {
        StSentence entity = new StSentence();
        entity.setStGroupId(groupId);
        return sentenceDao.queryCount(entity);
    }
    
    public StScore findScoreByPlanIdAndGroupIdAndUserId(int planId, int groupId, int userId) {
        StScore entity = new StScore();
        entity.setStPlanId(planId);
        entity.setStGroupId(groupId);
        entity.setUserId(userId);
        List<StScore> scores = scoreDao.findByColumns(entity);
        return scores.size() > 0 ? scores.get(0) : null;
    }
    
    public void saveScore(StScore score) {
        scoreDao.insert(score);
    }
    
    public void updateScore(StScore score) {
        scoreDao.update(score);
    }
    
    public int getRanking(StScore stScore, int planId, int groupId) {
        List<StScore> scores = scoreDao.orderByScore(planId, groupId);
        int ranking = 1;
        for (StScore item : scores) {
            if(item.getScore() == stScore.getScore()){
                break;
            }
            ranking++;
        }
        return ranking;
        
    }

	public void update(ClsSentence clsSentence) {
		clsSentenceDao.update(clsSentence);
	}

	public Pager findAllWords(Integer page, Integer rows, Integer id,
			String code, String sentence, String sentenceCn) {
		try {
			if(id != null){
				String id1 = URLDecoder.decode(id.toString(), "UTF-8");
				id = Integer.parseInt(id1);
			}
			if(code != null && code.length() >0)
				code = URLDecoder.decode(code, "UTF-8");
			if(sentence != null && sentence.length() >0)
				sentence = URLDecoder.decode(sentence, "UTF-8");
			if(sentenceCn != null && sentenceCn.length() >0)
				sentenceCn = URLDecoder.decode(sentenceCn, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Pager pager = clsSentenceDao.findWithCondition(page,rows,id,code,sentence,sentenceCn);
		return pager;
	}
	/**
	 * 判断某个字符串的编码
	 * @param str
	 * @return
	 */
	 public static String getEncoding(String str) {      
	        String encode = "GB2312";      
	       try {      
	           if (str.equals(new String(str.getBytes(encode), encode))) {      
	                String s = encode;      
	               return s;      
	            }      
	        } catch (Exception exception) {      
	        }      
	        encode = "ISO-8859-1";      
	       try {      
	           if (str.equals(new String(str.getBytes(encode), encode))) {      
	                String s1 = encode;      
	               return s1;      
	            }      
	        } catch (Exception exception1) {      
	        }      
	        encode = "UTF-8";      
	       try {      
	           if (str.equals(new String(str.getBytes(encode), encode))) {      
	                String s2 = encode;      
	               return s2;      
	            }      
	        } catch (Exception exception2) {      
	        }      
	        encode = "GBK";      
	       try {      
	           if (str.equals(new String(str.getBytes(encode), encode))) {      
	                String s3 = encode;      
	               return s3;      
	            }      
	        } catch (Exception exception3) {      
	        }      
	       return "";      
	    }      
}
